package com.taojiazheng.controller.index;

import com.taojiazheng.common.ResultResponse;
import com.taojiazheng.common.annotations.VisitorAccessible;
import com.taojiazheng.entity.History;
import com.taojiazheng.entity.Jobinfo;
import com.taojiazheng.service.history.HistoryService;
import com.taojiazheng.service.index.IndexService;
import com.taojiazheng.service.job.JobService;
import com.taojiazheng.service.user.FindUserService;
import com.taojiazheng.so.job.JobSo;
import com.taojiazheng.vo.history.HistoryVo;
import com.taojiazheng.vo.job.JobInfoVo;
import com.taojiazheng.vo.job.JobVo;
import com.taojiazheng.so.user.FindUserSo;
import com.taojiazheng.vo.user.FindUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 赵成林
 * @date 2017/11/22 23:26
 */
@Controller
public class IndexController {
    @Autowired
    private JobService jobService;

    @Autowired
    private IndexService indexService;

    @Autowired
    private FindUserService findUserService;

    @Autowired
    private HistoryService historyService;
    /**
     * 添加自定义注解
     * 实现不用登陆也可以访问
     *找工作
     * @return
     */ 
    @VisitorAccessible
    @ResponseBody
    @RequestMapping(value = "index/findjob", method = RequestMethod.POST)
    public ResultResponse indexFindJob(@RequestBody JobSo jobSo) {
      //return null;
        List<Jobinfo> list = indexService.getJobList(jobSo);
        ResultResponse resultResponse = new ResultResponse(list);
        return resultResponse;
    }
    /**
     * 找人
     * 用户列表
     */

    @VisitorAccessible
    @ResponseBody
    @RequestMapping(value = "index/finduser",method = RequestMethod.POST)
    public ResultResponse indexFindUser(){
        //return null;
        List<FindUserVo> getList = findUserService.getFindUserList();
        ResultResponse resultResponse = new ResultResponse(getList);
        return resultResponse;
    }
    /**
     * 找人
     * 详细信息
     */
    @VisitorAccessible
    @ResponseBody
    @RequestMapping(value = "userinfo",method = RequestMethod.POST)
    public ResultResponse findUserInfo(@RequestBody FindUserSo findUserSo){
        //return null;
        FindUserVo findUserVo = findUserService.getFindUserinfo(findUserSo);
        return new ResultResponse(findUserVo);
    }

    /**
     * 岗位的详细信息
     * 浏览次数增加
     * @param jobSo
     * @return
     */
    @VisitorAccessible
    @ResponseBody
    @RequestMapping(value = "jobinfo", method = RequestMethod.POST)
    public ResultResponse<JobInfoVo> jobinfo(@RequestBody JobSo jobSo) {
        JobInfoVo jobInfoVo = indexService.getJobInfo(jobSo);
        ResultResponse<JobInfoVo> resultResponse = new ResultResponse<JobInfoVo>(jobInfoVo);
        return resultResponse;
    }
    /**
     * 添加同步方法
     * 拆分数据更新接口
     */
    @VisitorAccessible
    @ResponseBody
    @RequestMapping(value = "updateNum",method =RequestMethod.POST)
    public ResultResponse updateVisitNum(@RequestBody JobSo jobSo){
        indexService.updateVisitNum(jobSo);
        return new ResultResponse();
    }
    /**
     * 创建新的工作
     * 需要登录
     * 前端传递token
     */
    @ResponseBody
    @RequestMapping(value = "create/job", method = RequestMethod.POST)
    public ResultResponse<JobVo> createJob(HttpServletRequest request, @RequestBody JobSo so) {
        JobVo jobVo = new JobVo();
        int jobId = jobService.createJob(so, request.getHeader("token"));
        jobVo.setJobId(jobId);
        ResultResponse resultResponse = new ResultResponse(jobVo);
        return resultResponse;
    }
    /**
     * 创建发布人
     * 需要登录
     * 前端传递token
     */
    @ResponseBody
    @RequestMapping(value = "create/user",method = RequestMethod.POST)
    public ResultResponse createUser(HttpServletRequest request, @RequestBody FindUserSo findUserSo){
        findUserService.createFindUser(findUserSo,request.getHeader("token"));
        return new ResultResponse();
    }

    @ResponseBody
    @RequestMapping(value = "create/history",method = RequestMethod.POST)
    public ResultResponse createHistory(HttpServletRequest request,@RequestBody JobSo so){
        History history = new History();
        history.setJobId(so.getJobId());
        historyService.createHistory(history,request.getHeader("token"));
        return new ResultResponse();
    }
    @ResponseBody
    @RequestMapping(value = "get/history",method = RequestMethod.POST)
    public ResultResponse getHistory(HttpServletRequest request){
        String token = request.getHeader("token");
        List<HistoryVo> historyVos = historyService.getHistory(token);
        return new ResultResponse(historyVos);
    }

    @ResponseBody
    @RequestMapping(value = "delete/history",method = RequestMethod.POST)
    public ResultResponse deleteHistory(HttpServletRequest request){
        String token = request.getHeader("token");
        historyService.deleteHistory(token);
        return  new ResultResponse();
    }
}
